package com.user.gamecenter.manager;

import android.util.Log;

import com.lzh.easythread.Callback;
import com.lzh.easythread.EasyThread;

/**
 * author : Administrator
 * e-mail : 254419013@qq.com
 * time   : 2017/05/02
 * desc   :
 */
public class ThreadManager {

    private static final String TAG = "ThreadManager";

    private final static EasyThread io;
    private final static EasyThread cache;
    private final static EasyThread calculator;
    private final static EasyThread file;

    public static EasyThread getIO() {
        return io;
    }

    public static EasyThread getCache() {
        return cache;
    }

    public static EasyThread getCalculator() {
        return calculator;
    }

    public static EasyThread getFile() {
        return file;
    }

    static {
        io = EasyThread.Builder
                .fixed(10)// 通过此三种方法指定所管理器所需要使用的线程池类型，对应Executors.newXXXThreadPool
                .name("IO")//线程名称
                .priority(7)//线程优先级
                .callback(new DefaultCallback()).build();
        cache = EasyThread.Builder.cacheable().name("cache").callback(new DefaultCallback()).build();
        calculator = EasyThread.Builder.fixed(4).name("calculator").priority(Thread.MAX_PRIORITY).callback(new DefaultCallback()).build();
        file = EasyThread.Builder.fixed(4).name("file").priority(3).callback(new DefaultCallback()).build();
    }

    private static class DefaultCallback implements Callback {

        @Override
        public void onError(Thread thread, Throwable t) {
            // 当使用EasyThread启动后台任务后，若在子线程运行过程中。出现了异常。将会将异常错误 t 回调通知到此方法中通知用户
            Log.e(TAG, "Task with thread " + thread + " has occurs an error: " + t.getMessage());
        }

        @Override
        public void onCompleted(Thread thread) {
            // 当使用EasyThread启动后台任务后，若子线程运行完毕。将会回调到此方法中通知用户
            Log.d(TAG, "Task with thread " + thread + " completed");
        }

        @Override
        public void onStart(Thread thread) {
            // 当子线程启动运行时，回调到此方法通知用户。
            Log.d(TAG, "Task with thread " + thread + " start running!");
        }
    }

}
